package com.xxr.mom.ci.charging.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xxr.mom.ci.charging.interworking.ApiService;
import com.xxr.mom.ci.common.dto.HlhtClientDto;
import com.xxr.mom.ci.common.dto.InterworkingParam;
import com.xxr.mom.ci.common.dto.InterworkingResult;
import com.xxr.mom.ci.common.dto.Result;
import com.xxr.mom.ci.common.enums.InterworkingRetEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author natsuki_kining
 */
@RestController
@RequestMapping
public class ApiController {

    @Autowired
    private ApiService service;

    /**
     * 对外暴露接口
     * 作为设备服务方提供对外的接口
     * 请求地址要求规范：http(s)://[域名]/evcs/v[版本号]/[接口名称]
     * @param httpServletRequest HttpServletRequest
     * @param method 请求方法
     * @param interworkingParam 请求体
     * @return 加密报文
     */
    @PostMapping("/evcs/vEternal/{method}")
    @SentinelResource(value = "vEternal", fallback = "vEternalFallback")
    public InterworkingResult vEternal(HttpServletRequest httpServletRequest, @PathVariable(name = "method") String method, @RequestBody InterworkingParam interworkingParam) {
        return service.accept(httpServletRequest, method, interworkingParam);
    }

    /**
     * 作为内部系统访问，我方请求对方，不对外暴露接口
     * @param hlhtClientDto 参数
     * @return
     */
    @PostMapping("/evcs/send")
    public Result<Object> send(@RequestBody HlhtClientDto hlhtClientDto) {
        return Result.success(service.send(hlhtClientDto));
    }

    /**
     * 熔断降级处理
     * @return
     */
    public InterworkingResult vEternalFallback() {
        return InterworkingResult.getInterworkingResult(InterworkingRetEnum.SYSTEM_BUSY);
    }
}
